<?php
/**
 * @title 100150. 移除后集合的最多元素数
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }


class Solution {

    /**
     * @param Integer[] $nums1
     * @param Integer[] $nums2
     * @return Integer
     */
    function maximumSetSize($nums1, $nums2) {
        /**
         * @since 2024-01-07 需要删除的元素数量
         */
        $needDeleteCount = count($nums1)>>1;

        /**
         * @since 2024-01-07 数组只保留每个元素1个
         */
        list($nums1, $deleteCount1) = $this->OneNumber($nums1);
        list($nums2, $deleteCount2) = $this->OneNumber($nums2);

        /**
         * @since 2024-01-07 nums1和nums2交集
         */
        $nums3 = array_intersect($nums1, $nums2);
        $nums3Count = count($nums3);

        /**
         * @since 2024-01-07 nums1, nums2足够删除
         */
        if($deleteCount1 >= $needDeleteCount AND $deleteCount2 >= $needDeleteCount){
            /**
             * @return Integer
             */
            return count($nums1) + count($nums2) - $nums3Count;
        } else {}

        /**
         * @since 2024-01-07 nums1，nums2都不够删
         */
        if($deleteCount1 < $needDeleteCount AND $deleteCount2 < $needDeleteCount){
            /**
             * @since 2024-01-07 删光公共元素，足够
             */
            if($deleteCount1+$nums3Count >= $needDeleteCount AND $deleteCount2+$nums3Count >= $needDeleteCount){
                /**
                 * @since 2024-01-07 数组1删除公共元素数量
                 */
                $del1 = $needDeleteCount-$deleteCount1;

                /**
                 * @since 2024-01-07 数组2删除公共元素数量
                 */
                $del2 = $needDeleteCount-$deleteCount2;

                /**
                 * @since 2024-01-07 数组1和数组2不同元素数量
                 */
                $totalCount = count($nums1) + count($nums2) - $nums3Count*2;

                /**
                 * @since 2024-01-07 公共元素保留下来的数量
                 */
                if($del1+$del2 <= $nums3Count){
                    $add = $nums3Count;
                } else {
                    $add = $nums3Count*2 - $del1 - $del2;
                }

                /**
                 * @since 2024-01-07 剩下的元素数量
                 */
                return $totalCount + $add;
            } else {}

            /**
             * @since 2024-01-07 至少有一个数组公共元素被删光
             * @since 2024-01-07 没有公共元素，nums1和nums2数量不会重复
             */
            return count($nums1) - ($needDeleteCount-$deleteCount1) + count($nums2) - ($needDeleteCount-$deleteCount2);

            // /**
            //  * @since 2024-01-07 nums1和nums2删光公共元素都不够
            //  */
            // if($deleteCount1+$nums3Count < $needDeleteCount AND $deleteCount2+$nums3Count < $needDeleteCount){
            //     return count($nums1) - ($needDeleteCount-$deleteCount1) + count($nums2) - ($needDeleteCount-$deleteCount2);
            // } else {}
            //
            // /**
            //  * @since 2024-01-07 只有一个数组删光公共元素不够
            //  * @since 2024-01-07 nums1数组删光公共元素不够
            //  */
            // if($deleteCount1+$nums3Count < $needDeleteCount){
            //     return count($nums1) - ($needDeleteCount-$deleteCount1) + count($nums2) - ($needDeleteCount-$deleteCount2);
            // } else {
            //     /**
            //      * @since 2024-01-07 nums2数组删光公共元素不够
            //      */
            //     return count($nums1) - ($needDeleteCount-$deleteCount1) + count($nums2) - ($needDeleteCount-$deleteCount2);
            // }
        } else {}

        /**
         * @since 2024-01-07 只有一个数组不够删除
         * @since 2024-01-07 nums1不够删除
         */
        if($deleteCount1 < $needDeleteCount){
            /**
             * @since 2024-01-07 nums1删除交集元素，足够删除
             */
            if($deleteCount1+$nums3Count >= $needDeleteCount){
                $nums1Count = count($nums1) - $nums3Count;
            } else {
                /**
                 * @since 2024-01-07 还要从nums1中删除，nums1无公共元素
                 */
                $nums1Count = count($nums1) - ($needDeleteCount-$deleteCount1);
            }
        } else {
            $nums1Count = count($nums1);
        }

        /**
         * @since 2024-01-07 num2不够删
         */
        if($deleteCount2 < $needDeleteCount){
            /**
             * @since 2024-01-07 num2删除交集元素，足够删除
             */
            if($deleteCount2+$nums3Count >= $needDeleteCount){
                $nums2Count = count($nums2) - $nums3Count;
            } else {
                /**
                 * @since 2024-01-07 还要从nums2中删除，nums2无公共元素
                 */
                $nums2Count = count($nums2) - ($needDeleteCount-$deleteCount2);
            }
        } else {
            $nums2Count = count($nums2);
        }

        /**
         * @return Integer
         */
        return $nums1Count+$nums2Count;
    }

    /**
     * @title 每个数，只保留1个
     * @author start2004
     * @since 2024-01-07 Created
     *
     * @param Integer[] $nums
     * @return [Integer[], Integer]
     */
    function OneNumber($nums){
        /**
         * @since 2024-01-07 哈希记录
         */
        $hashArray = [];

        /**
         * @since 2024-01-07 删除元素的数量
         */
        $deleteCount = 0;

        /**
         * @since 2024-01-07 遍历数组
         */
        foreach ($nums as $num){
            if(isset($hashArray[$num])){
                $deleteCount++;
            } else {
                $hashArray[$num] = $num;
            }
        }

        /**
         * @return [Integer[], Integer]
         */
        return [array_values($hashArray), $deleteCount];
    }
}



/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.07_4.php
 */
$datas = [
    // [[1,2,1,2], [1,1,1,1]], // 2
    // [[1,2,3,4,5,6], [2,3,2,3,2,3]], // 5
    // [[1,1,2,2,3,3], [4,4,5,5,6,6]], // 6
    [[2,4,1,4], [10,2,4,10]], // 4
    // [[1,5,10,8], [1,7,4,10]], // 4
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->maximumSetSize(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
